<template>
  <div class="param-main">
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
          <transition-group name="breadcrumb">
            <el-breadcrumb-item v-for="(item, index) in levelList" :key="index">
              <span>{{item.name}}</span>
            </el-breadcrumb-item>
          </transition-group>
        </el-breadcrumb> -->
    <el-form
      ref="tblMain"
      :model="serachForm"
      labelPosition="right"
      inline=""
      label-suffix=":"
      label-width="110px"
    >
      <el-row :span="24">
        <el-col :span="8">
          <el-form-item label="上级分类">
            <el-cascader
              v-model="topParent"
              :options="topParentList"
              @change="getChange"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分类名称">
            <el-input
              v-model="serachForm.name"
              size="small"
              placeholder=""
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="getList()"
          >查询</el-button
        >
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="detailTblMain(1)">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent(1)"
          >删除</el-button
        >
      </div>
    </el-form>
    <div style="height: calc(100% - 160px)">
      <el-table
        ref="tblMain"
        style="margin-top: 10px"
        :data="listData"
        height="100%"
        @selection-change="handleSelectionChange"
        @sort-change="getSortList"
        v-loading.body="loading"
        border
        stripe
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200px" >
          <template slot-scope="scope">
            <el-button  type="text" @click="detailTblMain(2,scope.row)" >编辑</el-button>
            <el-divider direction="vertical" ></el-divider>
            <el-button  type="text" @click="deleteEvent(scope.row)">删除</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button  type="text" @click="detailTblMain(3,scope.row)">明细</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="分类名称"
          align="center"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="parentName"
          label="上级名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="hot"
          label="热度"
          align="center"
        ></el-table-column>
      </el-table>
    </div>
    <!-- v-show="pageInfo.total != 0" -->
    <el-pagination
      style="margin-top: 10px"
      :current-page.sync="pageInfo.page"
      :page-size="pageInfo.size"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <add-part-classify
      ref="addPartClassify"
      v-if="visible"
      @ok="saveData"
      @cancle="cancle"
      :title="title"
      :row="rowData"
      :topParentList="topParentList"
    ></add-part-classify>
  </div>
</template>
<script>
import AddPartClassify from './addPartClassify'
import {
  addCarClassify,
  delCarClassify,
  upCarClassify,
  getCarClassifyList
} from '@/api/carLine'

export default {
  name: 'applyMain',
  components: {
    AddPartClassify
  },
  data() {
    return {
      visible: false,
      rowData: {},
      title: '',
      levelList: [],
      loading: false,
      listData: [],
      selection: [],
      serachForm: {
        topName: '',
        parentName: '',
        name: '',
        order:""
      },
      topParent: '',
      pageInfo: {
        page: 1,
        size: 10,
        total: 0
      },
      brandNames: [],
      topParentList: [] //上级分类树形数据
    }
  },
  watch: {
    pageData() {
      this.getList()
    }
  },
  created() {
    let data = this.$route.query.data
      ? this.$route.query.data
      : {
          name: '所有品牌',
          path: '/product/brand'
        } //考评申请id
    this.levelList.push(data)
  },
  mounted() {
    this.getTopParentList()
    this.getList()
  },
  methods: {
    /**
    *  排序查询
    */
    getSortList(column) {
      let arr = [];
      arr.push(column.prop);
      arr.push( column.order.substring(
           0,
           column.order.indexOf("ending")
       ))
      this.serachForm.order = arr.join(',');
      this.getList()
    },
    /**
     * 获取上级分类和一级分类
     */
    getChange(val) {
      this.serachForm.topName = val[0]
      this.serachForm.parentName = val[1]
    },
    /**
     * 获取上级分类和一级分类的树形结构数据
     */
    getTopParentList() {
      let param = {
        page: 1,
        size: 1000
      }
      getCarClassifyList(param)
        .then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            let topList = [],
              parentList = []
            data.content.map((item) => {
              if (topList.indexOf(item.topName) == -1) {
                topList.push(item.topName)
              }
              if (parentList.indexOf(item.parentName) == -1) {
                parentList.push(item.parentName)
              }
            })
            topList.forEach((el) => {
              let op = {
                value: el,
                label: el,
                children: []
              }
              data.content.map((item) => {
                if (el == item.topName) {
                  let index = parentList.indexOf(item.parentName)
                  if (index != -1) {
                    let opl = {
                      value: item.parentName,
                      label: item.parentName
                    }
                    parentList.splice(index, 1)
                    op.children.push(opl)
                  }
                }
              })
              this.topParentList.push(op)
            })
          }
        })
        .catch(() => {
          this.$message.error('服务器繁忙')
          this.loading = false
        })
    },
    /**
     * 按钮点击事件
     */
    detailTblMain(type,row) {
      if (type != 1) {
          this.title = type == 2 ? '修改配件分类' : '配件分类详情'
          this.rowData = JSON.parse(JSON.stringify(row))
      } else {
        this.title = '新增配件分类'
        this.rowData = {}
      }
      this.visible = true
    },
    /**
     * 保存数据接口
     */
    saveData(val) {
      if (this.title == '新增配件分类') {
        addCarClassify(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      } else {
        upCarClassify(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      }
    },
    /**
     * 关闭弹框
     */
    cancle() {
      this.visible = false
    },
    /**
     * 多选数据
     */
    handleSelectionChange(val) {
      this.selection = val
    },
    /**
     * 重置按钮
     */
    dataReset() {
      this.pageInfo.page = 1
      this.pageInfo.size = 10
      this.topParent = ''
      this.serachForm.topName = ''
      this.serachForm.parentName = ''
      this.serachForm.name = ''
      this.serachForm.order = ''
      this.getList()
    },
    /**
     * 分页
     */
    handleSizeChange(val) {
      this.pageInfo.size = val
      this.getList()
    },
    /**
     * 分页
     */
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.getList()
    },
    /**
     * 获取表格数据
     */
    getList() {
      let me = this
      me.selection = []
      let param = {
        page: me.pageInfo.page,
        size: me.pageInfo.size,
        ...me.serachForm
      }
      me.loading = true
      getCarClassifyList(param)
        .then((res) => {
          me.loading = false
          const { code = undefined, data = {} } = res.data
          if (!code) {
            me.listData = data.content
            me.pageInfo.total = data.totalElements
          }
        })
        .catch(() => {
          me.$message.error('服务器繁忙')
          me.loading = false
        })
    },
    /**
     * 删除数据
     */
    deleteEvent(row) {
      let ids = []
      if(row == '1'){
         if (this.selection.length < 1) {
          this.$message.warning('请至少选择一条数据')
          return
        }
        ids = this.selection.map(item=>{
          return item.id
        })
      }else{
        ids.push(row.id)
      }
      this.$confirm('此操作将永久删除该配件分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delCarClassify(ids).then((res) => {
            const { code = undefined, data = {} } = res.data
            if (!code) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.getList()
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
